package manage.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import manage.util.Result;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import manage.model.User;
import manage.service.UserService;
/**
* @author raphaelkxy
* @version 创建时间：2019.08.28
* @describe：登陆用的Controller，SpringSecurity的登陆会来这个方法中做验证
*/
@Controller
@RequestMapping(value = "/")
public class LoginController {
	@Resource
	private UserService<User> userServiceImpl;

	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String login(@RequestParam(value = "error", required = false) boolean error, ModelMap model) {
		if (error) {
			model.put("error", "用户名或密码错误");
		} else {
			model.put("error", "");
		}
		// System.out.println(userServiceImpl.selectByPrimaryKey("admin").getAuthority());
		return "login";
	}

	@ResponseBody
	@RequestMapping(value = "/logincheck", method = RequestMethod.POST)
	public Result login(@RequestBody User user, HttpSession httpSession) {
		User user1 = userServiceImpl.selectByPrimaryKey(user.getUsername());
		if(user1.getPassword().equals(user.getPassword())){
			httpSession.setAttribute("user",user1);
			httpSession.setAttribute("loginUser",user1);
			System.out.println(httpSession.getAttribute("loginUser"));
			return new Result(true,20000,"登陆成功");
		}else {
			return new Result(true,20002,"登陆失败");
		}
	}

	@RequestMapping(value = "/index",method = RequestMethod.GET)
	public String index() {
		return "index";
	}

	@RequestMapping(value = "/loginout")
	public String logout(HttpSession httpSession) {
		User user = (User)httpSession.getAttribute("user");
		System.out.println(user);
		return "redirect:/login";
	}


	/**
	 * 注销
	 */

	//@RequestMapping("logout")


}